﻿package com.msjt.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;

import com.msjt.base.BaseAction;
import com.msjt.entity.Industry;
import com.msjt.service.IIndustryService;
import com.msjt.util.DeleteFile;
import com.msjt.util.PageMode;

/**
 * name:IndustryAction function:主要行业Action author: kw time:2013.8.27
 */
@SuppressWarnings("serial")
public class IndustryAction extends BaseAction {
	// driver model
	private Industry industry;
	// 主页业务的接口
	// 分页MODEL
	private PageMode page;
	// 文件上传
	private List<File> file;
	private List<String> fileFileName;
	private List<String> fileContentType;

	// 文件上传所需属性end

	public PageMode getPage() {
		return page;
	}

	public List<File> getFile() {
		return file;
	}

	public void setFile(List<File> file) {
		this.file = file;
	}

	public List<String> getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(List<String> fileFileName) {
		this.fileFileName = fileFileName;
	}

	public List<String> getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(List<String> fileContentType) {
		this.fileContentType = fileContentType;
	}

	public void setPage(PageMode page) {
		this.page = page;
	}

	@Resource
	private IIndustryService industryService;

	public Industry getIndustry() {
		return industry;
	}

	public void setIndustry(Industry industry) {
		this.industry = industry;
	}

	public IIndustryService getIndustryService() {
		return industryService;
	}

	public void setIndustryService(IIndustryService industryService) {
		this.industryService = industryService;
	}

	public void splitPage(String actionPath) {
		if (this.page == null) {
			this.page = new PageMode();
			this.page.setPageIndex(1);
			this.page.setPageSize(10);
		}
		page.setAction(actionPath);
		page.setModelDrive("page");
		// 获取总页数
		int total = this.industryService.getIndustryCount();
		// 置入总页数
		page.setSumRows(total);
		// 创建分页链接
		page.createPageStr();
		List<Industry> industryList = this.industryService.getIndustryPage(
				page.getPageSize(), page.getPageIndex());
		// 将结果以及分页的链接放入请求中
		this.getRequest().put("industryList", industryList);
		this.getRequest().put("link", page.getPagingStr());
		 this.page=null;
	}

	// 查看更多页面分页显示
	public String getIndustryByPage() throws Exception {
		this.splitPage("industry_getIndustryByPage.html");
		return "showMore";
	}

	// 后台管理页面分页显示
	public String getIndustyPageAfter() throws Exception {
		this.splitPage("industry_getIndustyPageAfter.html");
		return "showPage";
	}

	// 添加主营信息
	public String addIndustry() throws Exception {
		// 上传文件
		// 得到工程保存图片的路径
		String realpath = ServletActionContext.getServletContext().getRealPath(
				"/uploadImages");
		// 用时间来命名
		Date date = new Date();
		// 循环上传的文件
		String name = "";
		for (int i = 0; i < file.size(); i++) {
			// 存入文件名
			name = date.getTime() + (new Random()).nextInt(10000)
					+ fileFileName.get(i);
			InputStream is = new FileInputStream(file.get(i));
			// 得到图片保存的位置(根据realpath来得到图片保存的路径在tomcat下的该工程里)
			File destFile = new File(realpath, name);
			// 把图片写入到上面设置的路径里
			OutputStream os = new FileOutputStream(destFile);
			byte[] buffer = new byte[400];
			int length = 0;
			while ((length = is.read(buffer)) > 0) {
				os.write(buffer, 0, length);
			}

			is.close();
			os.close();
		}
		String path = "uploadImages/" + name;
		path = path.replace("\\", "/");
		// 上传文件路径接收结束
		this.industry.setInPicPath(path);
		boolean flag = this.industryService.addIndustry(this.industry);
		if (!flag) {
			this.getRequest().put("msg",
					"<script>alert('添加失败');history.go(-1);</script>");
			return "error";
		}
		return getIndustyPageAfter();
	}

	// 修改之前需要加载信息
	public String preUpdate() throws Exception {
		Industry ind = this.industryService.getIndustryById(this.industry);
		this.getRequest().put("industry", ind);
		return "update";
	}

	// 修改主营信息
	// 图片名称用原来的
	public String updateIndustry() throws Exception {
		String name = "";
		if (file != null) {
			// 上传文件
			// 得到工程保存图片的路径
			String path = ServletActionContext.getServletContext().getRealPath(
					"");
			// 循环上传的文件
			// 删除原文件
			// 还需要删除服务器上的图片，以免服务器项目臃肿
			String picture = path + "/" + this.industry.getInPicPath();
			picture = picture.replace("\\", "/");
			// 删除服务器的图片
			DeleteFile dFile = new DeleteFile();
			dFile.deleteFolder(picture);
			Date date = new Date();
			// 循环上传的文件
			String realpath = ServletActionContext.getServletContext()
					.getRealPath("/uploadImages");
			for (int i = 0; i < file.size(); i++) {
				name = date.getTime() + (new Random()).nextInt(10000)
						+ fileFileName.get(i);
				InputStream is = new FileInputStream(file.get(i));
				// 得到图片保存的位置(根据realpath来得到图片保存的路径在tomcat下的该工程里)
				File destFile = new File(realpath, name);
				// 把图片写入到上面设置的路径里
				OutputStream os = new FileOutputStream(destFile);
				byte[] buffer = new byte[400];
				int length = 0;
				while ((length = is.read(buffer)) > 0) {
					os.write(buffer, 0, length);
				}

				is.close();
				os.close();
			}
			String spath = "uploadImages/" + name;
			spath = spath.replace("\\", "/");
			// 上传文件路径接收结束
			this.industry.setInPicPath(spath);
		}
		boolean flag = this.industryService.updateIndustry(this.industry);
		if (!flag) {
			this.getRequest().put("msg",
					"<script>alert('修改失败');history.go(-1);</script>");
			return "error";
		}
		return getIndustyPageAfter();
	}

	// 删除主营信息
	public String deleteIndustry() throws Exception {
		String path = ServletActionContext.getServletContext().getRealPath("");
		// 还需要删除服务器上的图片，以免服务器项目臃肿
		Industry ind = this.industryService.getIndustryById(this.industry);
		String picture = path + "/" + ind.getInPicPath();
		picture = picture.replace("\\", "/");
		// 删除服务器的图片
		DeleteFile dFile = new DeleteFile();
		dFile.deleteFolder(picture);
		// 删除图片结束
		boolean flag = this.industryService.deleteIndustry(this.industry);
		if (!flag) {
			this.getRequest().put("msg",
					"<script>alert('删除失败');history.go(-1);</script>");
			return "error";
		}
		return getIndustyPageAfter();
	}

	// 批量删除
	public String deleteMore() throws Exception {
		String idString = ServletActionContext.getRequest().getParameter(
				"idString");
		//取出","号
		idString = idString.substring(0, idString.length() - 1);
		List<String> industryList=this.industryService.getIndustryPath(idString);
		String path = ServletActionContext.getServletContext().getRealPath("");
		DeleteFile dFile = new DeleteFile();
		if(industryList!=null){
			for (int i = 0; i < industryList.size(); i++) {			
				// 还需要删除服务器上的图片，以免服务器项目臃肿
				String picture = path + "/" + industryList.get(i);
				picture = picture.replace("\\", "/");
				// 删除服务器的图片
				dFile.deleteFolder(picture);
			}
		}
		boolean flag = this.industryService.deleteMore(idString);
		if (!flag) {
			this.getRequest().put("msg",
					"<script>alert('删除失败！');history.go(-1);</script>");
			return "error";
		}
		return getIndustyPageAfter();
	}

	// 得到一个主营信息，根据行业ID
	public String getIndustryById() throws Exception {
		Industry ind = this.industryService.getIndustryById(this.industry);
		this.getRequest().put("industry", ind);
		return "showZyhy";
	}

	// 得到一个主营信息，根据行业ID
	public String findInd() throws Exception {
		Industry ind = this.industryService.getIndustryById(this.industry);
		this.getRequest().put("industry", ind);
		return "industryDetail";
	}
}
